package org.example.pet.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.pet.dto.PetAddDTO;
import org.example.pet.dto.PetInfoResponseDTO;
import org.example.pet.dto.PetPageQueryDTO;
import org.example.pet.mapper.PetMapper;
import org.example.pet.pojo.Pet;
import org.example.pet.pojo.PetBreed;
import org.example.pet.service.PetBreedService;
import org.example.pet.service.PetService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PetServiceImpl extends ServiceImpl<PetMapper, Pet> implements PetService {
    @Autowired
    private PetBreedService petBreedService;
    
    @Override
    public Page<PetInfoResponseDTO> page(int page, int pageSize) {
        // 创建分页构造器对象
        Page<Pet> pageInfo = new Page<>(page,pageSize);
        Page<PetInfoResponseDTO> PetInfoResponseDTOPage = new Page<>();

        // 条件构造器
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();
        
        // 添加排序条件
        queryWrapper.orderByDesc(Pet::getPetId);

        // 执行分页查询
        this.page(pageInfo,queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,PetInfoResponseDTOPage,"records");


        // 将品种id转为品种名称并封装
        List<Pet> records = pageInfo.getRecords();
        List<PetInfoResponseDTO> list = records.stream().map((item) -> {
            PetInfoResponseDTO petDto = new PetInfoResponseDTO();

            BeanUtils.copyProperties(item,petDto);

            // 根据品种id查询品种名称
            Long breedId = item.getBreedId();
            LambdaQueryWrapper<PetBreed> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PetBreed::getBreedId, breedId);
            PetBreed breed = petBreedService.getOne(wrapper);

            if(breed != null){
                String breedName = breed.getBreedName();
                petDto.setBreedName(breedName);
            }
            return petDto;
        }).collect(Collectors.toList());

        PetInfoResponseDTOPage.setRecords(list);
        
        return PetInfoResponseDTOPage;
    }

    @Override
    public Page<PetInfoResponseDTO> page(PetPageQueryDTO queryDTO) {
        int page = queryDTO.getPage() == null ? 1 : queryDTO.getPage();
        int pageSize = queryDTO.getPageSize() == null ? 10 : queryDTO.getPageSize();
        
        // 创建分页构造器对象
        Page<Pet> pageInfo = new Page<>(page,pageSize);
        Page<PetInfoResponseDTO> PetInfoResponseDTOPage = new Page<>();

        // 条件构造器
        LambdaQueryWrapper<Pet> queryWrapper = new LambdaQueryWrapper<>();

        // 添加过滤条件
        if(StringUtils.hasText(queryDTO.getName())) {
            queryWrapper.like(Pet::getName, queryDTO.getName());
        }
        
        if(StringUtils.hasText(queryDTO.getStatus())) {
            queryWrapper.eq(Pet::getStatus, queryDTO.getStatus());
        }
        
        if(StringUtils.hasText(queryDTO.getGender())) {
            queryWrapper.eq(Pet::getGender, queryDTO.getGender());
        }

        // 添加排序条件
        queryWrapper.orderByDesc(Pet::getPetId);

        // 执行分页查询
        this.page(pageInfo,queryWrapper);

        // 对象拷贝
        BeanUtils.copyProperties(pageInfo,PetInfoResponseDTOPage,"records");


        // 将品种id转为品种名称并封装
        List<Pet> records = pageInfo.getRecords();
        List<PetInfoResponseDTO> list = records.stream().map((item) -> {
            PetInfoResponseDTO petDto = new PetInfoResponseDTO();

            BeanUtils.copyProperties(item,petDto);

            // 根据品种id查询品种名称
            Long breedId = item.getBreedId();
            LambdaQueryWrapper<PetBreed> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(PetBreed::getBreedId, breedId);
            PetBreed breed = petBreedService.getOne(wrapper);

            if(breed != null){
                String breedName = breed.getBreedName();
                petDto.setBreedName(breedName);
            }
            return petDto;
        }).collect(Collectors.toList());

        PetInfoResponseDTOPage.setRecords(list);

        return PetInfoResponseDTOPage;
    }

    @Override
    public PetInfoResponseDTO getPetDetail(Long petId) {
        LambdaQueryWrapper<Pet> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Pet::getPetId, petId);
        Pet pet = this.getOne(wrapper);

        LambdaQueryWrapper<PetBreed> breedWrapper = new LambdaQueryWrapper<>();
        breedWrapper.eq(PetBreed::getBreedId, pet.getBreedId());
        PetBreed petBreed = petBreedService.getOne(breedWrapper);

        PetInfoResponseDTO res = new PetInfoResponseDTO();
        BeanUtils.copyProperties(pet, res);
        res.setBreedName(petBreed.getBreedName());
        
        return res;
    }

    @Override
    public void addPet(PetAddDTO dto) {
        LambdaQueryWrapper<PetBreed> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PetBreed::getBreedName, dto.getBreedName());
        Long breedId = petBreedService.getOne(wrapper).getBreedId();
        Pet pet = new Pet();
        BeanUtils.copyProperties(dto, pet);
        pet.setBreedId(breedId);
        save(pet);
    }
}
